package com.example.hzzrpcclient.config.netty;

import com.example.hzzrpcclient.util.GuardedObject;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author hzz
 * @version 1.0
 * @date 2020/3/26 15:47
 */
public class NettyClientHandler extends ChannelInboundHandlerAdapter {


    private static final Logger log = LoggerFactory.getLogger(NettyClientHandler.class);


    public NettyClientHandler() {

    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        // ctx.writeAndFlush(firstMessage);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        Channel channel = ctx.channel();
        String text = new String((byte[]) msg);
        System.out.println("接收到数据:"+ text);
        boolean fireEvent = GuardedObject.fireEvent(text.split(";")[0], text);
        System.out.println("接收结果:"+fireEvent);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        Channel channel = ctx.channel();

        log.error("netty链接抛出异常");
        ctx.close();//释放资源
    }
}
